<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /**
       * 类的组成由 构造器、属性（实例属性、静态属性）、方法（实例方法、静态方法） 组成
       */
      class Animal {
        // 实例属性（个性），只有实例才能访问
        name = "";
        age = 0;

        // 静态属性（共性），只能通过类名(Animal)去访问
        static hometown = "";

        // 构造器，简化赋值
        constructor(name, age) {
          this.name = name;
          this.age = age;
        }

        // 实例方法（个性），有实例才能访问
        eat(food) {
          console.log(`${this.name} 吃 ${food}`);
        }

        // 静态方法（共性），只能通过类名(Animal)去访问
        static sleep() {
          console.log("所有动物都是闭着眼睛睡觉的~");
        }
      }

      /**
      const a1 = new Animal();
      a1.name = "张三";
      a1.age = 11;
      console.log(a1);

      const a2 = new Animal();
      a2.name = "李四";
      a2.age = 22;
      console.log(a2);
      **/
      /**
      const a1 = new Animal("张三丰", 111);
      console.log(a1);
      const a2 = new Animal("李四", 222);
      console.log(a2);
      **/
      // 对象 === 实例
      const a1 = new Animal("旺财", 12);
      a1.eat("骨头");

      Animal.hometown = "地球";
      console.log(`所有动物的栖息地是 ${Animal.hometown}`);

      Animal.sleep();

      //   console.log(typeof Animal);
    </script>
  </body>
</html>
